package chanfoofoo;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;


public class User_manager {

	private User user_action(String in_action , String user_id  , String user_pwd) {
		User resultUser = null ;
		PersistenceManager pm = PMF01.get().getPersistenceManager(); 

		String sqlcmd = "user_id == '" + user_id + "'" ; 
		Query query = pm.newQuery(User.class, sqlcmd) ; 
	    List<User> tmp_list = (List<User>)query.execute() ;  
	    if 	((tmp_list != null) && (tmp_list.size() == 1)) {
	    	resultUser = pm.detachCopy(tmp_list.get(0)) ;
	    }
	    if (in_action.equals("USER_GET")) {
	    	if ( resultUser == null || ! user_pwd.equals(resultUser.getUser_pwd()) ) {
	    		resultUser = null ; 
	    	}	    	
	    }
	    if (in_action.equals("USER_CREATE")) { 
	    	if (resultUser != null) { 
	    		resultUser = null ;
	    	} else {
	    		resultUser = new User() ; 
	    		resultUser.setUser_id(user_id) ; 
	    		resultUser.setUser_pwd(user_pwd) ; 
	    		pm.makePersistent(resultUser) ;
	    		resultUser = user_action("USER_GET" , user_id , user_pwd) ;
	    	}	    		
	    }
	    pm.close() ;	    		
		return resultUser ; 
	}
		
	public User getUser(String user_id  , String user_pwd) {
		return user_action("USER_GET" , user_id , user_pwd) ;
	}
	public User addUser(String user_id  , String user_pwd) {
		return user_action("USER_CREATE" , user_id , user_pwd) ;
	}	
	public User getUserInfo(String user_id) {
		return user_action("USER_GET_INFO" , user_id , "") ;
	}
	
	public User getUserxx(String user_id  , String user_pwd) {
		 
		User resultUser = null ;
		PersistenceManager pm = PMF01.get().getPersistenceManager();

//		   Query query = pm.newQuery("select from Employee " +
//                   "where lastName == lastNameParam " +
//                   "parameters String lastNameParam " +
//                   "order by hireDate desc");
//
//List<Employee> results = (List<Employee>) query.execute("Smith");		
//		  Query query = pm.newQuery(Employee.class,
//          "lastName == 'Smith' ");		

//		Query query = pm.newQuery("select from User" + 
//				"where user_id == p1 && user_pwd == p2 " + 
//				" parameters String p1 , p2 ") ; 
//	    Query query = pm.newQuery(Employee.class,
//        "lastName == 'Smith' order by hireDate desc");		 
		// String sqlcmd = "user_id == '" + user_id + "' && user_pwd == '" + user_pwd +"'" ; 
		String sqlcmd = "user_id == '" + user_id + "'" ; 
		Query query = pm.newQuery(User.class, sqlcmd) ; 
	    List<User> tmp_list = (List<User>)query.execute() ;  
	    if 	((tmp_list != null) && (tmp_list.size() == 1)) {
	    	resultUser = pm.detachCopy(tmp_list.get(0)) ;
//	    	 List tmp_list = (List)pm.newQuery(query).execute();
//	 	    list_StockQuote = (List<StockQuote>) pm.detachCopyAll(tmp_list); 	    	
	    	 // list_StockQuote = (List<StockQuote>) pm.detachCopyAll(tmp_list); 
	    	if ( ! user_pwd.equals(resultUser.getUser_pwd()) ) {
	    		resultUser = null ; 
	    	}
	    }
		return resultUser ; 
	}
	public String saveUser(User in_User) {
		PersistenceManager pm = PMF01.get().getPersistenceManager();
		System.out.println("saveUser : " + in_User.getUser_id() + in_User.getEmail_list() ) ; 
		pm.makePersistent(in_User) ;
		pm.close() ; 
		return "OK" ; 
	}

	public String addUserxx(String user_id  , String user_pwd) {
		PersistenceManager pm = PMF01.get().getPersistenceManager();
		User tmp_User = new User() ; 
		tmp_User.setUser_id(user_id) ; 
		tmp_User.setUser_pwd(user_pwd) ; 
		pm.makePersistent(tmp_User) ;
		pm.close() ;
		pm = PMF01.get().getPersistenceManager();
		return "OK" ;  
	}
}
